package com.wm.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wm.reggie.common.CustomException;
import com.wm.reggie.dto.SetmealDto;
import com.wm.reggie.entity.Setmeal;
import com.wm.reggie.entity.SetmealDish;
import com.wm.reggie.mapper.SetmealMapper;
import com.wm.reggie.service.SetmealDishService;
import com.wm.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 保存套餐和套餐菜品信息
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息
        this.save(setmealDto);

        //得到Setmeal_dish的信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //补充上套餐id的信息
        List<SetmealDish> list = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //进行保存
        setmealDishService.saveBatch(list);

    }

    /**
     * 根据id查询套餐信息和套餐菜品信息
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查出套餐的基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        //查出套餐的菜品信息
        //创建条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);


        return setmealDto;
    }

    /**
     * 修改套餐的信息和套餐菜品信息
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐的基本信息
        this.updateById(setmealDto);

        //修改套餐里的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            SetmealDish setmealDish = new SetmealDish();
            BeanUtils.copyProperties(item, setmealDish);
            return setmealDish;
        }).collect(Collectors.toList());

        //1.删除套餐里菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmealDto.getId() != null, SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //2.插入新套餐菜品的信息
        setmealDishService.saveBatch(setmealDishes);


    }

    /**
     * 删除套餐信息和菜品信息
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {

        //判断套餐是否可用
        LambdaQueryWrapper<Setmeal> setmealqueryWrapper =  new LambdaQueryWrapper<>();
        setmealqueryWrapper.in(Setmeal::getId,ids);
        setmealqueryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(setmealqueryWrapper);
        if(count > 0){
            throw new CustomException("套餐正在出售，不能删除");
        }

        //删除套餐的基本信息
        this.removeByIds(ids);

        //删除套餐的菜品信息
        ids.forEach((item) -> {
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId, item);
            setmealDishService.remove(queryWrapper);
        });

    }
}
